import django_filters

from .models import QuestionCategory, Question, Questionnaire, ModuleTeam


class QuestionCategoryFilter(django_filters.rest_framework.FilterSet):
    module = django_filters.CharFilter(empty_value=0, method='filter_module')     # 没传模块id默认返回空数据

    def filter_module(self, queryset, name, value):
        if not value:   # 没传的话, 值为上面的empty_value值
            return queryset.none()
        if value in ['all']:
            return queryset.all()
        return queryset.filter(module_id=value)

    class Meta:
        model = QuestionCategory
        fields = ['module', ]


class QuestionFilter(django_filters.rest_framework.FilterSet):
    module = django_filters.CharFilter(empty_value=0, method='filter_module')     # 没传模块id默认返回空数据

    def filter_module(self, queryset, name, value):
        if not value:   # 没传的话, 值为上面的empty_value值
            return queryset.none()
        if value in ['all']:
            return queryset.all()
        return queryset.filter(module_id=value)

    class Meta:
        model = Question
        fields = ['module', ]


class QuestionnaireFilter(django_filters.rest_framework.FilterSet):
    module = django_filters.CharFilter(empty_value=0, method='filter_module')     # 没传模块id默认返回空数据

    def filter_module(self, queryset, name, value):
        if not value:   # 没传的话, 值为上面的empty_value值
            return queryset.none()
        if value in ['all']:
            return queryset.all()
        return queryset.filter(module_id=value)

    class Meta:
        model = Questionnaire
        fields = ['module', ]


class ModuleTeamFilter(django_filters.rest_framework.FilterSet):
    module = django_filters.CharFilter(empty_value=0, method='filter_module')     # 没传模块id默认返回空数据

    def filter_module(self, queryset, name, value):
        if not value:   # 没传的话, 值为上面的empty_value值
            return queryset.none()
        if value in ['all']:
            return queryset.all()
        return queryset.filter(module_id=value)

    class Meta:
        model = ModuleTeam
        fields = ['module', ]
